package cn.itsource.org.service.impl;

import cn.itsource.basic.dto.ShopDto;
import cn.itsource.basic.exception.BusinessException;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.BaiduAuditUtils;
import cn.itsource.basic.util.Md5Utils;
import cn.itsource.basic.util.StrUtils;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.domain.ShopAuditLog;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.mapper.ShopAuditLogMapper;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.org.service.IShopService;
import cn.itsource.user.domain.Logininfo;
import cn.itsource.user.mapper.LogininfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void settlement(Shop shop) {

        if(!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BusinessException("店铺名不合法");
        }

        if(!StringUtils.isEmpty(shop.getLogo())){
            if (!BaiduAuditUtils.ImgCensor("http://123.207.27.208" + shop.getLogo())){
                throw new BusinessException("图片不合法");
            }
        }

        //一：检验 - 空值校验：前端的js校验容易被禁用，不安全，后端也要做校验
        if(StringUtils.isEmpty(shop.getName())){ //空和空字符串返回true
            throw new BusinessException("提交资料不能为空!!!");
        }

        //二：校验 - 当前店铺是否入驻过
        Shop dbShop = shopMapper.findByNameAndAddress(shop);
        if(dbShop != null){//查到了 = 入驻过
            throw new BusinessException("请勿重复入驻，忘记密码请联系管理员!!!");
        }

        Employee admin = shop.getAdmin();

        //2.2.处理密码和盐值
        //获取随机盐值
        String salt = StrUtils.getComplexRandomString(32);
        //获取加密之后的密码
        String md5Pwd = Md5Utils.encrypByMd5(admin.getPassword()+salt);
        admin.setSalt(salt);
        admin.setPassword(md5Pwd);

        //2.3.保存员工信息
        employeeMapper.add(admin);

        //2.4.保存店铺信息t_shop
        shop.setAdminId(admin.getId());
        shopMapper.add(shop);

        //2.5.保存登录信息t_logininfo
        Logininfo logininfo = employee2Logininfo(admin);
        logininfoMapper.add(logininfo);

        //2.6.将店铺的id更新到t_employee中
        admin.setShopId(shop.getId());
        //2.6.将登录信息的id更新到t_employee中
        admin.setLogininfoId(logininfo.getId());
        //2.7.修改员工信息 - 修改shop_id和logininfo_id
        employeeMapper.update(admin);

    }

    public Logininfo employee2Logininfo(Employee employee){
        Logininfo logininfo = new Logininfo();

        BeanUtils.copyProperties(employee,logininfo);
        //当前对象是店铺管理员
        logininfo.setType(0);
        return logininfo;
    }

    @Override
    public void pass(ShopAuditLog log) throws MessagingException {
        //改状态 保存审核日志 发邮件
        Shop shop = shopMapper.findById(log.getShopId());
        shop.setState(2);
        shopMapper.update(shop);

        //保存审核日志
        log.setState(2);
        //log.setAuditId("审核人=登陆人");
        shopAuditLogMapper.add(log);

        //发邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        helper.setFrom("898106099@qq.com");
        helper.setSubject("店铺激活邮件");
        //true - 能够再内容中编写html标签 - 会解析
        helper.setText("<h3>你的店铺已经审核通过，请<a href='http://localhost:8080/shop/active/"+log.getShopId()
                +"'>点击这里</a>激活邮件</h3>", true);
        //店铺管理员
        Employee admin = employeeMapper.findById(shop.getAdminId());
        helper.setTo(admin.getEmail());
        //发送
        javaMailSender.send(mimeMessage);
    }

    @Override
    public void reject(ShopAuditLog log) throws MessagingException {
        Shop shop = shopMapper.findById(log.getShopId());
        shop.setState(4);
        shopMapper.update(shop);

        //保存审核日志
        log.setState(4);
        //log.setAuditId("审核人=登陆人");
        shopAuditLogMapper.add(log);

        //发邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        helper.setFrom("898106099@qq.com");
        helper.setSubject("店铺激活失败通知");
        //true - 能够再内容中编写html标签 - 会解析
        helper.setText("<h3>你的店铺审核未通过，请<a href='http://localhost:8081/#/regiseterEdit/"+log.getShopId()
                +"'>点击这里</a>修改入驻信息重新提交</h3>", true);
        //店铺管理员
        Employee admin = employeeMapper.findById(shop.getAdminId());
        helper.setTo(admin.getEmail());
        //发送
        javaMailSender.send(mimeMessage);
    }

    @Override
    public List<ShopDto> getCountByState() {
        return shopMapper.getCountByState();
    }

}
